@page "/Loan/{License}"
@using Rental.Data.Context
@inject RentalContext RentalContext

<Navigation></Navigation>

<AuthorizeView Roles="Administrator">
    <Authorized>
        
        <table class="table">
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">User</th>
                <th scope="col">Date Start</th>
                <th scope="col">Date End</th>
                <th scope="col">Status</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var loan in _loans)
            {
                <tr>
                    <th scope="row">@loan.Serial</th>
                    <td>@GetUsernameFromLoan(loan)</td>
                    <td>@loan.DateStart.ToString("yyyy-MM-dd")</td>
                    <td>@loan.DateEnd.ToString("yyyy-MM-dd")</td>
            
                    @if (IsCanceled(@loan.HasBeenCanceled))
                    {
                        <td><span class="badge bg-danger">Canceled</span></td>
                    }
                    else if (IsFuture(@loan.DateStart))
                    {
                        <td><span class="badge bg-primary">Future</span></td>
                    }
                    else if (IsActive(@loan.DateEnd))
                    {
                        <td><span class="badge bg-success">Active</span></td>
                    }
                    else
                    {
                        <td><span class="badge bg-secondary">Inactive</span></td>
                    }
                </tr>
            }
            </tbody>
        </table>
    </Authorized>
    <NotAuthorized>
        <MessageNotAuthorized/>
    </NotAuthorized>
</AuthorizeView>

<Footer></Footer>

@code {

    [Parameter]
    public string License { get; set; }

    private List<Rental.Data.Loan> _loans;

    protected override async Task OnInitializedAsync()
    {
        _loans = await RentalContext.GetLoansByLicense(License);
    }

    private void VerifyAndUpdateDates(int serial, DateTime start, DateTime end)
    {
        // Greater than zero - This date start is later than date end (wrong format).
        if (start.CompareTo(end) > 0)
        {
            // Interchange the dates for set the right format.
            RentalContext.UpdateLoanDates(serial, end.ToString("yyyy-MM-dd"), start.ToString("yyyy-MM-dd"));
        }
    }

    private string GetUsernameFromLoan(Rental.Data.Loan loan)
    {
        return RentalContext.GetUserByCreditCardNumber(loan.CreditCardNumber).Result.Username;
    }

    private bool IsCanceled(string isCanceled)
    {
        if (isCanceled.Equals("TRUE"))
        {
            return true;
        }
        
        return false;
    }
    
    private bool IsFuture(DateTime start)
    {
        if (start.CompareTo(DateTime.Today) > 0)
        {
            return true;
        }

        return false;
    }

    private bool IsActive(DateTime end)
    {
        if (end.CompareTo(DateTime.Today) >= 0)
        {
            return true;
        }

        return false;
    }

}